﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading.Tasks;
using X23.Application.MenuManagement;
using X23.Application.MenuManagement.Dto;
using X23.Extensions.AttributeExtensions;
using X23.Model.Const;
using X23.Model.Menu;
using X23.Util.CacheUtil;
using X23.Util.LinqUtil;
using X23.Util.SerializationUtil;

namespace X23.Controllers
{
    /// <summary>
    /// 菜单管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MenuController : BaseApiController
    {
        private readonly IMenuManagementService _menuManagementService;
        private readonly ICacheService _cacheService;

        public MenuController(IMenuManagementService menuManagementService,ICacheService cacheService)
        {
            _menuManagementService = menuManagementService;
            _cacheService = cacheService;
        }

        /// <summary>
        /// 获取所有控制器名称
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [UnitOfWork(IsDisabled = true)]
        public async Task<List<string>> GetAllControllerName()
        {
            static Task<string> func() => Task.Run(async () =>
            {
                var allControllerName = typeof(Startup).Assembly.GetTypes()
                    .Where(type => typeof(BaseApiController).IsAssignableFrom(type) && type != typeof(BaseApiController))
                    .Select(x => x.Name)
                    .ToList().ToJson();
                return await Task.FromResult(allControllerName);
            });
            var result = await _cacheService.GetOrCreateAsync("ControllerKey", func);
            return result.ToObjet<List<string>>();
        }
        /// <summary>
        /// 获取所有按钮名称
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        [UnitOfWork(IsDisabled = true)]
        public async Task<List<string>> GetButtonName()
        {
            static Task<string> func() => Task.Run(async () =>
            {

                var allButtonName = typeof(ButtonConst).GetFields().Select(x => x.GetValue(null)).ToList().ToJson();
                return await Task.FromResult(allButtonName);
            });
            var result = await _cacheService.GetOrCreateAsync("ButtonKey", func);
            return result.ToObjet<List<string>>();
        }
        /// <summary>
        /// 菜单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<MenuTreeDto>> GetMenuTree()
        {
            return await _menuManagementService.GetMenuTree();
        }

        #region CRUD
        /// <summary>
        /// 查询菜单列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageData<MenuDto>> QueryData(QueryDto input)
        {
            Expression<Func<Menu, bool>> predicate = x => string.IsNullOrEmpty(input.Name) || x.Name.Contains(input.Name);
            var result = await _menuManagementService.GetPageDataAsync(predicate, input.Pages, input.Rows);
            return result;
        }
        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<MenuDto> Add(MenuDto menu)
        {
            return await _menuManagementService.AddAsync(menu);
        }
        /// <summary>
        /// 修改菜单
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<MenuDto> Update(MenuDto menu)
        {
            return await _menuManagementService.UpdateAsync(menu);
        }
        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="menuId">菜单id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<bool> Delete(int menuId)
        {
            return await _menuManagementService.DeleteAsync(menuId);
        }
        #endregion

    }
}
